/**
 * @name        hookEmitt.ts - EventBus 事件总线
 * @author      shenyc
 * @date        2020-05-06 - ?
 * @copyright   Copy right (c) shenyc (shenyczz@163.com).
 *              All rights reserved.
 *
 ******************************************************************************/
// {{@@@}}
"use strict"
// ----------------------------------------------------------------------------
import mitt from "mitt"
import { onBeforeUnmount } from "vue"

interface MittOption {
    /** 事件名称 */
    name: string
    /** 回调函数 */
    callback: Fn
}

/**
 * 必须在 hookEmitt 函数体外面
 */
const emitter = mitt()

/**
 * 事件总线钩子
 * @param option
 * @returns
 */
export const hookEmitt = (option?: MittOption) => {
    if (option) {
        emitter.on(option.name, option.callback)

        /** 挂载之前先关闭 */
        onBeforeUnmount(() => {
            emitter.off(option.name)
        })
    }

    return { emitter }
}
export default hookEmitt
// ----------------------------------------------------------------------------
// {{@@@}}
